/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entity.MUser;
import entity.SrcCureplanDoctor;
import entity.SrvCureplan;
import entity.SrvCureservice;
import entity.MSpecialist;
import session.MSpecialistFacade;
import entity.MHotel;
import session.MHotelFacade;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author CHANWUT
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SrvCurePlanManager {

    @PersistenceContext(unitName = "MedicalHub-warPU")
    private EntityManager em;
    @Resource
    private SessionContext context;
    @EJB
    private SrvCureplanFacade srvCureplanFacade;
    @EJB
    private MUserFacade mUserFacade;
    @EJB
    private SrvCureserviceFacade srvCureserviceFacade;
    @EJB
    private MSpecialistFacade mSpecialistFacade;
    @EJB
    private MHotelFacade mHotelFacade;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public SrvCureplan SaveCurePlanDetail(String txtSymptom, String txtAllergicHistory, String txtDiseaseHistory, String uplAttachHistory, String txtRoomType, String txtPriceMinAccept, String txtPriceMaxAccept, String datDateMinAccept, String datDateMaxAccept, String chkRequestQuotation, String ddlHotel, String ddlSpecialist, String cureServiceId, String curePlanId, String btnSubmit, String btnSaveDraft) {
        try {
            MUser mUser = mUserFacade.find((long)1);
            if (mUser == null) {
                //@TODO : Throw Exception message error
            }
            //Check if has 'curePlanId'
            if (!curePlanId.isEmpty()) cureServiceId = srvCureplanFacade.find(Long.parseLong(curePlanId)).getSrvCureservice().getId().toString();
            SrvCureservice srvCureservice = srvCureserviceFacade.find(Long.parseLong(cureServiceId));
            if (srvCureservice == null) {
                //@TODO : Throw Exception message error
            }
            MSpecialist mSpecialist = null;
            MHotel mHotel = null;
            if(!ddlSpecialist.isEmpty())
            mSpecialist = mSpecialistFacade.find(Long.parseLong(ddlSpecialist));
            if(!ddlHotel.isEmpty())
            mHotel = mHotelFacade.find(Long.parseLong(ddlHotel));

            String strStatus = "D";
            if (!btnSubmit.isEmpty()) {
                strStatus = "W";
            } else if (!btnSaveDraft.isEmpty()) {
                strStatus = "D";
            } else {
                //@TODO : Throw Exception message error
            }

            SrvCureplan srvCureplan = saveSrvCureplan(txtSymptom, txtAllergicHistory, txtDiseaseHistory, uplAttachHistory, txtRoomType, txtPriceMinAccept, txtPriceMaxAccept, datDateMinAccept, datDateMaxAccept, chkRequestQuotation, mHotel, mSpecialist, cureServiceId, curePlanId, mUser, srvCureservice, strStatus);
            if (!btnSubmit.isEmpty()) {
                addSrvCureplanDoctor(srvCureplan); //Only add No Update
            }
            em.flush();
            em.refresh(srvCureplan);
            return srvCureplan;
        } catch (Exception e) {
            context.setRollbackOnly();
            return null;
        }
    }

    private SrvCureplan saveSrvCureplan(String txtSymptom, String txtAllergicHistory, String txtDiseaseHistory, String uplAttachHistory, String txtRoomType, String txtPriceMinAccept, String txtPriceMaxAccept, String datDateMinAccept, String datDateMaxAccept, String chkRequestQuotation, MHotel mHotel, MSpecialist mSpecialist, String cureServiceId, String curePlanId, MUser mUser, SrvCureservice srvCureservice, String strStatus) throws ParseException {
        SrvCureplan srvCureplan;
        if (curePlanId.isEmpty()) { //New
            srvCureplan = new SrvCureplan();
        } else { //Update
            srvCureplan = srvCureplanFacade.find(Long.parseLong(curePlanId));
            //@TODO : ถ้าไม่มี object ให้ Throw Exception message error
        }
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);

        srvCureplan.setSymptom(txtSymptom);
        srvCureplan.setAllergicHistory(txtAllergicHistory);
        srvCureplan.setDiseaseHistory(txtDiseaseHistory);
        srvCureplan.setAttachHistory(uplAttachHistory);
        srvCureplan.setRoomType(txtRoomType);
        if (!txtPriceMinAccept.isEmpty()) {
            srvCureplan.setPriceMinAccept(new BigDecimal(txtPriceMinAccept));
        } else {
            srvCureplan.setPriceMinAccept(null);
        }
        if (!txtPriceMaxAccept.isEmpty()) {
            srvCureplan.setPriceMaxAccept(new BigDecimal(txtPriceMaxAccept));
        } else {
            srvCureplan.setPriceMaxAccept(null);
        }
        if (!datDateMinAccept.isEmpty()) {
            srvCureplan.setDateMinAccept(df.parse(datDateMinAccept));
        } else {
            srvCureplan.setDateMinAccept(null);
        }
        if (!datDateMaxAccept.isEmpty()) {
            srvCureplan.setDateMaxAccept(df.parse(datDateMaxAccept));
        } else {
            srvCureplan.setDateMaxAccept(null);
        }
        srvCureplan.setRequestQuotation(chkRequestQuotation.equals("on"));
        srvCureplan.setMUser(mUser);
        srvCureplan.setSrvCureservice(srvCureservice);
        srvCureplan.setStatus(strStatus);
        srvCureplan.setMSpecialist(mSpecialist);
        srvCureplan.setMHotel(mHotel);

        em.persist(srvCureplan);
        return srvCureplan;
    }

    private void addSrvCureplanDoctor(SrvCureplan srvCureplan) {

        // set up customer order
        SrcCureplanDoctor srcCureplanDoctor;
        if (srvCureplan != null) {
            List<SrcCureplanDoctor> lsrcCureplanDoctor = (List<SrcCureplanDoctor>) srvCureplan.getSrcCureplanDoctorCollection();
            if (lsrcCureplanDoctor.isEmpty()) {
                srcCureplanDoctor = new SrcCureplanDoctor();
                srcCureplanDoctor.setSrvCureplan(srvCureplan);

                em.persist(srcCureplanDoctor);
            } else {
                //Not insert do noting
            }
        } else {
            //@TODO : Throw Exception message error
        }

    }
}
